package com.ruoyi.ruoyixingyue.modules.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.page.PageData;
import com.ruoyi.ruoyixingyue.modules.api.entity.Course;
import com.ruoyi.ruoyixingyue.modules.api.entity.Order;
import com.ruoyi.ruoyixingyue.modules.api.entity.Student;
import com.ruoyi.ruoyixingyue.modules.api.service.ICourseService;
import com.ruoyi.ruoyixingyue.modules.api.service.IOrderService;
import com.ruoyi.ruoyixingyue.modules.api.service.IStudentService;
import com.ruoyi.ruoyixingyue.modules.api.vo.OrderListVo;
import com.ruoyi.ruoyixingyue.modules.api.vo.OrderUpdateVo;
import com.ruoyi.ruoyixingyue.modules.utils.Condition;
import com.ruoyi.ruoyixingyue.modules.utils.QueryPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzz
 * @since 2024-04-06
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private ICourseService courseService;

    @PostMapping("/order/list/API_001")
    public R orderListApi_001(@RequestBody Condition<OrderListVo> condition){
        QueryPage queryPage = condition.getPage();
        OrderListVo listVo = condition.getCondition();

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Objects.nonNull(listVo.getId()),Order::getId,listVo.getId())
                .eq(Objects.nonNull(listVo.getPrice()),Order::getPrice,listVo.getPrice())
                .orderByDesc(Order::getCreateTime);
        if (StringUtils.isNotBlank(listVo.getCourseName())) {
            List<Course> courseList = courseService.list(new QueryWrapper<Course>().lambda().like(Course::getName, listVo.getCourseName()));
            List<Long> courseIdList = courseList.stream().map(Course::getId).collect(Collectors.toList());
            queryWrapper.lambda().in(Order::getCourseId,courseIdList);
        }

        if (StringUtils.isNotBlank(listVo.getUserName())) {
            List<Student> studentList = studentService.list(new QueryWrapper<Student>().lambda()
                    .like(Student::getUserName, listVo.getUserName()));
            List<Long> studentIdList = studentList.stream().map(Student::getId).collect(Collectors.toList());
            queryWrapper.lambda().in(Order::getUserId,studentIdList);

        }
        List<Order> list = orderService.list(queryWrapper);
        list.forEach(order -> {
            Student student = studentService.getById(order.getUserId());
            order.setUserName(student.getUserName());
            Course course = courseService.getById(order.getCourseId());
            order.setCourseName(course.getName());
        });
        Page<Order> resultPage = new Page<Order>().setCurrent(1).setSize(10);
        orderService.page(resultPage,queryWrapper);
        return R.ok(new PageData<>(list, resultPage.getTotal()));
    }

    @PostMapping("/order/getById/API_002/{id}")
    public R orderGetByIdApi_002(@PathVariable("id") Long id){
        Order order = orderService.getById(id);
        Student student = studentService.getById(order.getUserId());
        order.setUserName(student.getUserName());
        Course course = courseService.getById(order.getCourseId());
        order.setCourseName(course.getName());
        return R.ok(order);
    }

    @PostMapping("/order/update/API_003")
    public R orderUpdateApi_003(@RequestBody OrderUpdateVo orderUpdateVo){
        Long orderId = orderUpdateVo.getId();
        Order order = orderService.getById(orderId);
        order.setCourseId(orderUpdateVo.getCourseId());
        order.setPrice(orderUpdateVo.getPrice());
        order.setUserId(orderUpdateVo.getUserId());
        orderService.updateById(order);
        return R.ok("修改成功");
    }

    @PostMapping("/order/delete/API_004/{id}")
    public R orderGetByIdApi_004(@PathVariable("id") Long id){
        boolean b = orderService.removeById(id);
        if (b){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }

    }

}
